package com.reny.szkj.mvvmarts.glide

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.bumptech.glide.request.target.ImageViewTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.reny.git.imgload.BlurTransformation
import com.reny.git.imgload.GlideRoundTransform
import com.reny.git.imgload.glide.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File

class GlideAppLoader: ILoader {

    override fun display(
        view: View,
        model: Any?,
        owner: Any,
        header: HashMap<String, String>,
        radius: Int,
        isBlur: Boolean,
        placeHolder: Int,
        errorHolder: Int,
        fallbackHolder: Int
    ) {
        var request = GlideApp.with(view)
        when (owner) {
            is FragmentActivity -> {
                request = GlideApp.with(owner)
            }
            is Activity -> {
                request = GlideApp.with(owner)
            }
            is Fragment -> {
                request = GlideApp.with(owner)
            }
        }

        var req = request
            .load(configUrlHeader(model, header))
            .placeholder(placeHolder)
            .error(errorHolder)
            .fallback(fallbackHolder)

        if (radius > 0) {
            req = req.transform(GlideRoundTransform(radius))//req.optionalCircleCrop().override(radius * 2)
        }
        if (isBlur) {
            try {
                req = req.optionalTransform(
                    BlurTransformation(view.context)
                )
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        if (view is ImageView) {
            req.into(view)
        } else { //针对LinearLayout等控件设置背景
            req.intoBg(view)
        }
    }

    override fun displayTarget(
        view: ImageView,
        model: Any?,
        target: ImageViewTarget<Bitmap>,
        owner: Any
    ) {
        GlideApp.with(view)
            .asBitmap()
            .load(model)
            .into(target)
    }

    override fun cache(context: Context, model: Any?, width: Int?, height: Int?) {
        try {
            val req = GlideApp.with(context)
                .download(configUrlHeader(model))

            if (width != null && height != null) {
                req.preload(width, height)
            } else {
                req.preload()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }



    override fun displayCache(view: View, model: Any?) {
        try {
            val req = GlideApp.with(view)
                .load(configUrlHeader(model))
                .placeholder(ImgUtils.instance.placeHolder)
                .error(ImgUtils.instance.errorHolder)
                .fallback(ImgUtils.instance.fallbackHolder)
                .onlyRetrieveFromCache(true)
            if (view is ImageView) {
                req.into(view)
            } else {
                req.intoBg(view)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            //调用这个方法要求先调用cache  如果缓存被清理了 可能出现问题 这里兜底方案
            display(view, model)
        }
    }

    override fun loadBitmap(context: Context, model: Any?, callBack: (Bitmap?) -> Unit) {
        try {
            GlideApp.with(context)
                .asBitmap().load(configUrlHeader(model))
                .into(object : SimpleTarget<Bitmap?>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap?>?
                    ) {
                        callBack(resource)
                    }
                    override fun onLoadFailed(errorDrawable: Drawable?) {
                        callBack(null)
                    }
                })
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun loadDrawable(
        context: Context,
        model: Any?,
        callBack: (Drawable?) -> Unit
    ) {
        try {
            GlideApp.with(context)
                .asDrawable().load(configUrlHeader(model))
                .into(object : SimpleTarget<Drawable?>() {
                    override fun onResourceReady(
                        resource: Drawable,
                        transition: Transition<in Drawable?>?
                    ) {
                        callBack(resource)
                    }

                    override fun onLoadFailed(errorDrawable: Drawable?) {
                        callBack(null)
                    }
                })
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun downLoadImg(context: Context, model: Any?, callBack: (File?) -> Unit) {
        try {
            GlobalScope.launch {
                GlideApp.with(context)
                    .download(configUrlHeader(model))
                    .into(object : SimpleTarget<File?>() {
                        override fun onResourceReady(
                            resource: File,
                            transition: Transition<in File?>?
                        ) {
                            callBack(resource)
                        }
                    })
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun pauseRequests(context: Context) {
        GlideApp.with(context).pauseRequests()
    }

    override fun resumeRequests(context: Context) {
        GlideApp.with(context).resumeRequests()
    }
}